package com.pic.mgt.monitor.parser.syushin;

import org.apache.commons.lang.StringUtils;

import com.pic.mgt.monitor.WrongUnitFormatException;
import com.pic.mgt.monitor.parser.FormatParser;

public class SyushinMessageBusinessNameParser extends FormatParser {
	
	private int realMessageFilenameLen;
	private int bucketMessageFilenameLen;
	private int realMessageBusinessNameStartAtChar;
	private int realMessageBusinessNameEndToChar;
	private int bucketMessageBusinessNameStartAtChar;
	private int bucketMessageBusinessNameEndToChar;
	private String messageConfirmationFilenameSuffix;
	
	@Override
	public Object parse(Object _confirmationFilename) throws WrongUnitFormatException {
		// 確認是否為 String 型態
		// throws WrongUnitFormatException
		super.checkObjectType(_confirmationFilename, String.class);
		
		String confirmationFilename = (String)_confirmationFilename;
		
		String businessName = null;
		
		// 取得交易檔案檔名的長度
		int transactionFilenameLen = StringUtils.removeEnd(confirmationFilename,
														   this.messageConfirmationFilenameSuffix)
														   .length();
		
		if (transactionFilenameLen == this.realMessageFilenameLen) {
			// 表示為 real 的message
			
			// 取得 business name
			try {
				businessName = confirmationFilename.substring(
													this.realMessageBusinessNameStartAtChar - 1,
													this.realMessageBusinessNameEndToChar);
			} catch (IndexOutOfBoundsException e) {
				// do nothgin
			}
		}
		else if (transactionFilenameLen == this.bucketMessageFilenameLen) {
			// 表示為 bucket 的message
			
			// 取得 business name
			try {
				businessName = confirmationFilename.substring(
													this.bucketMessageBusinessNameStartAtChar - 1,
													this.bucketMessageBusinessNameEndToChar);
			} catch (IndexOutOfBoundsException e) {
				// do nothgin
			}
		}
		
		if (null != businessName) {
			return businessName;
		}
		else {
			throw new WrongUnitFormatException(
					"Confirmation filename '" + confirmationFilename + "' format is invalid.");
		}
	}
	
	// --- getter and setter ---

	public void setRealMessageFilenameLen(int realMessageFilenameLen) {
		this.realMessageFilenameLen = realMessageFilenameLen;
	}

	public void setBucketMessageFilenameLen(int bucketMessageFilenameLen) {
		this.bucketMessageFilenameLen = bucketMessageFilenameLen;
	}

	public void setRealMessageBusinessNameStartAtChar(
			int realMessageBusinessNameStartAtChar) {
		this.realMessageBusinessNameStartAtChar = realMessageBusinessNameStartAtChar;
	}

	public void setRealMessageBusinessNameEndToChar(
			int realMessageBusinessNameEndToChar) {
		this.realMessageBusinessNameEndToChar = realMessageBusinessNameEndToChar;
	}

	public void setBucketMessageBusinessNameStartAtChar(
			int bucketMessageBusinessNameStartAtChar) {
		this.bucketMessageBusinessNameStartAtChar = bucketMessageBusinessNameStartAtChar;
	}

	public void setBucketMessageBusinessNameEndToChar(
			int bucketMessageBusinessNameEndToChar) {
		this.bucketMessageBusinessNameEndToChar = bucketMessageBusinessNameEndToChar;
	}

	public void setMessageConfirmationFilenameSuffix(
			String messageConfirmationFilenameSuffix) {
		this.messageConfirmationFilenameSuffix = messageConfirmationFilenameSuffix;
	}

}
